﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Admin.Models.ConfigManager;
using Zocono.WCS.Admin.Models.User;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Application.AppTraces;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;

namespace Zocono.WCS.Admin.Controllers.ConfigManageMoudle
{
    public class StationInfoController : BaseController
    {
        private readonly StationInfoApp _stationInfoApp;

        public StationInfoController(StationInfoApp stationInfoApp, IMemoryCache memoryCache, TracesApp tracesApp, IHttpContextAccessor httpContextAccessor) : base(memoryCache, tracesApp, httpContextAccessor)
        {
            this._stationInfoApp = stationInfoApp;
        }

        public IActionResult Index()
        {
            return View();
        }

        public ActionResult StationInfoAsync()
        {
            //await AddTracesInfo();
            return View();
        }
        /// <summary>
        /// 获取所有工位列表
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> GetAllStationInfoAsync()
        {
            var where = new QueryStationInfoDto();
            where.PageIndex = (Start / Length) + 1;
            where.PageSize = Length;
            where.StationCode = Request.Form["code"].ToString();
            where.StationName = Request.Form["name"].ToString();

            var stationInfos = await _stationInfoApp.GetPagedStationInfoAsync(where);
            //手动转换成Dto
            var stationInfoDtos = new List<StationInfoDto>();
            if (stationInfos.Result != null && stationInfos.Result.Count > 0)
            {
                foreach (var stationInfo in stationInfos.Result)
                {
                    stationInfoDtos.Add(new StationInfoDto()
                    {
                        ID = stationInfo.ID,
                        StationCode = stationInfo.StationCode,
                        StationName = stationInfo.StationName
                    });
                }
            }
            
            var pageData = new TableData<StationInfoDto>(Draw, stationInfos.Total, stationInfos.Total, stationInfoDtos);
            //await AddTracesInfo(5, 0, "获取工位数据");
            return Json(pageData);
        }
        /// <summary>
        /// 添加编辑工位配置页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> StationInfoAddOrEditAsync(long id)
        {
            var stationInfo = new StationInfo();
            if (id > 0)
            {
                stationInfo = await _stationInfoApp.GetStationInfoByIdAsync(id);
            }
            return View(stationInfo);
        }

        public async Task<JsonResult> OperateStationInfoAsync()
        {
            var stationInfo = new StationInfo();
            stationInfo.ID = Request.Form["id"].ToInt64();
            stationInfo.StationCode = Request.Form["StationCode"].ToString();
            stationInfo.StationName = Request.Form["StationName"].ToString();

            bool result;
            string msg;

            try
            {
                if (stationInfo.ID == 0)
                {
                    result = await _stationInfoApp.CreateStationInfoAsync(stationInfo);
                    await AddTracesInfo(2, 0, "新增工位配置");
                }
                else
                {
                    result = await _stationInfoApp.ModifyStationInfoAsync(stationInfo);
                    await AddTracesInfo(3, 0, "修改工位配置");
                }
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }

            return CreateJsonGetResult(result, msg);
        }

        /// <summary>
        /// 根据ID删除数据
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> DeleteStationInfoAsync()
        {
            var id = Request.Form["id"].ToInt64();
            bool result;
            string msg;
            try
            {
                result = await _stationInfoApp.DeleteStationInfoByIdAsync(id);
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }
            await AddTracesInfo(4, 0, "删除工位配置");
            return CreateJsonGetResult(result, msg);
        }
    }
}
